import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/entity/role_info.dart';
import 'package:winkai/ext/screen_ext.dart';
import 'package:winkai/helper/chat_helper.dart';
import 'package:winkai/res/dimens.dart';
import 'package:winkai/ui/discover/controller/discover_child_controller.dart';
import 'package:winkai/utils/net_image_load_utils.dart';
import 'package:winkai/widget/empty_layout.dart';

class DiscoverChildPage extends StatefulWidget {
  const DiscoverChildPage({
    super.key,
    required this.index,
  });

  final int index;

  @override
  State<DiscoverChildPage> createState() => _DiscoverChildPageState();
}

class _DiscoverChildPageState extends State<DiscoverChildPage> {
  late DiscoverChildController controller;

  @override
  void initState() {
    super.initState();
    Get.create<DiscoverChildController>(() => DiscoverChildController());
    controller = Get.find<DiscoverChildController>();
    controller.index = widget.index;
    controller.refreshData();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DiscoverChildController>(
        init: controller,
        global: false,
        builder: (_) {
          return controller.stateType != null
              ? EmptyLayout(type: controller.stateType!)
              : EasyRefresh.builder(
                  onRefresh: () async {
                    controller.refreshData();
                  },
                  onLoad: controller.isLoadMore
                      ? () async {
                          controller.loadMore();
                        }
                      : null,
                  childBuilder: (context, physics) {
                    return CustomScrollView(
                      physics: physics,
                      slivers: [
                        controller.recordList.isEmpty
                            ? SliverToBoxAdapter(
                                child: SizedBox(
                                  width: double.infinity,
                                  height: 400.w,
                                  child: EmptyLayout(
                                    type: controller.isNoNetwork
                                        ? EmptyType.noNetwork
                                        : EmptyType.empty,
                                    onReload: () {
                                      controller.stateType = EmptyType.loading;
                                      controller.update();
                                      controller.refreshData();
                                    },
                                    physics:
                                        const NeverScrollableScrollPhysics(),
                                  ),
                                ),
                              )
                            : _buildGridView(context, controller.recordList),
                      ],
                    );
                  },
                );
        });
  }

  Widget _buildGridView(BuildContext context, List<RoleInfo> list) {
    return SliverPadding(
      padding: EdgeInsets.only(
          left: Dimens.mainHorizontalSpacing,
          top: 8.w,
          right: Dimens.mainHorizontalSpacing,
          bottom: 8.w + navigationHeight),
      sliver: SliverGrid(
        delegate: SliverChildBuilderDelegate(
          (context, index) {
            return _buildItem(list[index]);
          },
          childCount: list.length,
        ),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          childAspectRatio: 0.769953051643192,
          crossAxisSpacing: 7.w,
          mainAxisSpacing: 7.w,
          crossAxisCount: 2,
        ),
      ),
    );
  }

  Widget _buildItem(RoleInfo item) {
    var width = (screenWidth - 47.w) / 2;
    var height = width / 0.769953051643192;
    return GestureDetector(
      onTap: () {
        ChatHelper().jumpToChatPage(item);
      },
      child: Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(16),
            border: Border.all(
              width: 1.w,
              color: Colors.black,
            )),
        child: Stack(
          alignment: Alignment.bottomCenter,
          children: [
            NetImageLoadUtils.load(item.avatar, width, height,
                isSetRadius: true, radius: 15),
            Container(
              height: 50.w,
              decoration: const BoxDecoration(
                  borderRadius: BorderRadius.vertical(
                      top: Radius.circular(6), bottom: Radius.circular(15)),
                  color: Color(0x66000000)),
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          item.name ?? '',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                      Container(
                        width: 32.w,
                        height: 14.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(7.w),
                          color: const Color(0x33fdffee),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          item.age.toString(),
                          style: TextStyle(
                            fontSize: 10.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    width: double.infinity,
                    child: Text(
                      item.aboutMe ?? '-',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 10.sp,
                        color: const Color(0xb3ffffff),
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
